﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace ComputerizedRehabForms
{
    public partial class BellsResultsForm : Form
    {
        int [] test_results;
        private FolderBrowserDialog SaveTo = new FolderBrowserDialog();
        private string saveDirectory;

        public BellsResultsForm()
        {
            InitializeComponent();
        }



        private void BellsResultsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void BellsResultsForm_Load(object sender, EventArgs e)
        {
            generateData();
        }

        private void ews_button_Click(object sender, EventArgs e)
        {
            DataManager.showExit();
        }

        private void save_button_Click(object sender, EventArgs e)
        {
            /*
             * User clicks the save results button
             * First, let user select folder to save results to
             * Next, screen cap results page, save as pdf (or jpeg)
             * Next, save txt file with numerical data
             */

            SaveTo.ShowDialog();
            saveDirectory = SaveTo.SelectedPath; // user selects folder to save files to        

            string fileName = saveDirectory + "/bells_" + DataManager.CID + "_" + DataManager.MRN + "_" + DateTime.Now.ToString("MM-dd-yyyy") + "_" + DateTime.Now.ToString("HH-mm") + ".jpeg"; //create file name. NOTE: should follow specified naming format - CID_MRN_DATE_TIME.jepg

            System.Threading.Thread.Sleep(1000);// wait 1 second for dialog box to close before saving

            //screen shot results page
            //DataManager.resultImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            //DataManager.g = Graphics.FromImage(DataManager.resultImage);
            //DataManager.g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            DataManager.resultImage.Save(fileName, ImageFormat.Jpeg); //save screenshot to directory as a JPEG


            /*
             * This are will be where we create the txt file of numerical data
             */
            int total_found = 0;
            int left_found = 0;
            int right_found = 0;
            int center_found = 0;
            for (int i = 0; i < test_results.Length; i++)
            {
                total_found += test_results[i];
                if (i < 3)
                {
                    left_found += test_results[i];
                }
                else if (i < 4)
                {
                    center_found += test_results[i];
                }
                else
                {
                    right_found += test_results[i];
                }
            }

            StreamWriter sw = File.CreateText(saveDirectory + "/bells_" + DataManager.CID + "_" + DataManager.MRN + "_" + DateTime.Now.ToString("MM-dd-yyyy") + "_" + DateTime.Now.ToString("HH-mm") + ".txt");
            sw.WriteLine("C1, " + test_results[0] + "/5");
            sw.WriteLine("C2, " + test_results[1] + "/5");
            sw.WriteLine("C3, " + test_results[2] + "/5");
            sw.WriteLine("C4, " + test_results[3] + "/5");
            sw.WriteLine("C5, " + test_results[4] + "/5");
            sw.WriteLine("C6, " + test_results[5] + "/5");
            sw.WriteLine("C7, " + test_results[6] + "/5");
            sw.WriteLine("LEFT, " + left_found + "/15");
            sw.WriteLine("CENTER, " + center_found + "/5");
            sw.WriteLine("RIGHT, " + right_found + "/15");
            sw.WriteLine("TOTAL, " + total_found + "/35");
            sw.Close();

            DataManager.showIntro();
        }

        public void generateData()
        {
            test_results = DataManager.getBellsResults();
            int total_found = 0;
            for (int i = 0; i < test_results.Length; i++)
            {
                total_found += test_results[i];
            }
            c1_res_label.Text = test_results[0].ToString() + "/5";
            c2_res_label.Text = test_results[1].ToString() + "/5";
            c3_res_label.Text = test_results[2].ToString() + "/5";
            c4_res_label.Text = test_results[3].ToString() + "/5";
            c5_res_label.Text = test_results[4].ToString() + "/5";
            c6_res_label.Text = test_results[5].ToString() + "/5";
            c7_res_label.Text = test_results[6].ToString() + "/5";
            total_res_label.Text = total_found.ToString() + "/35";
        }
    }
}
